{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "570c0d91-abdc-481b-b624-3415225ce1b4",
   "metadata": {},
   "source": [
    "### self的用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在执行构造方法\n",
      "<__main__.Person object at 0x000002480A1293D0> 正在学Python\n",
      "正在执行构造方法\n",
      "<__main__.Person object at 0x000002480A1053D0> 正在学Python\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self):\n",
    "        print(\"正在执行构造方法\")\n",
    "    # 定义一个study()实例方法\n",
    "    def study(self):\n",
    "        print(self,\"正在学Python\")\n",
    "zhangsan = Person()\n",
    "zhangsan.study()\n",
    "lisi = Person()\n",
    "lisi.study()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "study() 中的 self 代表该方法的调用者，即谁调用该方法，那么 self 就代表谁"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">Python 类方法中的 self 参数就相当于 C++ 中的 this 指针。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.Person object at 0x000002480A129A60>\n",
      "<__main__.Person object at 0x000002480A129A60>\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def who(self):\n",
    "        print(self)\n",
    "zhangsan = Person()\n",
    "#第一种方式\n",
    "zhangsan.who()\n",
    "#第二种方式\n",
    "who = zhangsan.who\n",
    "who()#通过 who 变量调用zhangsan对象中的 who() 方法\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
